<?php


namespace App\Modules\Admin\Entities\Seller;


use App\Models\Model;
use Illuminate\Support\Facades\DB;

class SellerRoleModel extends Model
{
    protected $table = 'seller_roles';
    protected $primaryKey = 'seller_role_id';
    protected $is_delete = 0; //是否开启删除（1.开启删除，就是直接删除；0.假删除）
    protected $delete_field = 'is_delete'; //删除字段


    public function menus()
    {
        return $this->belongsToMany(SellerMenuModel::class, 'seller_role_with_menus', 'seller_role_id', 'menu_id')
            ->where(['is_delete' => 0, 'is_check' => 1])
            ->orderBy('menu_sort', 'ASC')
            ;

    }
    public function getSelectLists($where=[]){
        $first = ['seller_role_id','seller_role_id as id','role_name','seller_id','parent_id','is_type'];
        return list_to_tree($this->where($where)->get($first)->toArray(),'seller_role_id','parent_id');
    }
    /**
     * @Function         grantMenus
     * @chineseAnnotation:给角色赋予权限
     * @englishAnnotation:
     */
    public function grantMenus($menus)
    {
        return SellerRoleWithMenu::create($menus);
    }

    /**
     * @Function         deleteMenus
     * @chineseAnnotation:取消角色赋予的权限
     * @englishAnnotation:
     */
    public function deleteMenus($menus)
    {
        return SellerRoleWithMenu::where($menus)->delete();
    }
}